package web.weibo.controller;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.servlet.ModelAndView;
import web.weibo.mapper.MapperUser;
import web.weibo.model.*;

import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
public class AspectAuth {

    public static void log(String format, Object... args) {
        System.out.println(String.format(format, args));
    }

    @Autowired
    private MapperUser mapperUser;

    private ModelAndView isLogin(ProceedingJoinPoint joinPoint) {
        System.out.println("最简单的方法匹配");
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(
                RequestAttributes.REFERENCE_REQUEST
        );
        User currentUser = ControllerHelper.currentUser(this.mapperUser, request);
        if (currentUser.role != UserRole.guest) {
            log("currentUser %s", currentUser);
            try {
                return (ModelAndView) joinPoint.proceed();
            } catch (Throwable throwable) {
                throw new RuntimeException(throwable);
            }
        } else {
            return new ModelAndView("redirect:/login/view");
        }
    }

    @Around("execution(* web.weibo.controller.ControllerWeibo.timeline(..))")
    public ModelAndView loginRequired(ProceedingJoinPoint joinPoint) {
        return isLogin(joinPoint);
    }

    @Around("execution(* web.weibo.controller.ControllerWeibo.add(..))")
    public ModelAndView weiboAdd(ProceedingJoinPoint joinPoint) {
        return isLogin(joinPoint);
    }

    @Around("execution(* web.weibo.controller.ControllerWeibo.commentAdd(..))")
    public ModelAndView commentAdd(ProceedingJoinPoint joinPoint) {
        return isLogin(joinPoint);
    }

    @Around("execution(* web.weibo.controller.ControllerWeibo.simple(..))")
    public ModelAndView myWeibo(ProceedingJoinPoint joinPoint) {
        return isLogin(joinPoint);
    }

    @Around("execution(* web.weibo.controller.ControllerWeibo.myComments(..))")
    public ModelAndView myComment(ProceedingJoinPoint joinPoint) {
        return isLogin(joinPoint);
    }

    @Around("execution(* web.weibo.controller.ControllerWeibo.delete(..))")
    public ModelAndView weiboDeleteRequired(ProceedingJoinPoint joinPoint) {
        return isLogin(joinPoint);
    }

    @Around("execution(* web.weibo.controller.ControllerWeibo.edit(..))")
    public ModelAndView weiboEditRequired(ProceedingJoinPoint joinPoint) {
        return isLogin(joinPoint);
    }

    @Around("execution(* web.weibo.controller.ControllerWeibo.follow(..))")
    public ModelAndView followRequired(ProceedingJoinPoint joinPoint) {
        return isLogin(joinPoint);
    }

    @Around("execution(* web.weibo.controller.ControllerWeibo.Fans(..))")
    public ModelAndView Fans(ProceedingJoinPoint joinPoint) {
        return isLogin(joinPoint);
    }

    @Around("execution(* web.weibo.controller.ControllerWeibo.commentDelete(..))")
    public ModelAndView commentDelete(ProceedingJoinPoint joinPoint) {
        return isLogin(joinPoint);
    }

    @Around("execution(* web.weibo.controller.ControllerWeibo.commentEdit(..))")
    public ModelAndView commentEdit(ProceedingJoinPoint joinPoint) {
        return isLogin(joinPoint);
    }

    // TODO权限判断
    @Around("execution(* web.weibo.controller.ControllerTodo.delete(..))")
    public ModelAndView todoDelete(ProceedingJoinPoint joinPoint) {
        return isLogin(joinPoint);
    }

    @Around("execution(* web.weibo.controller.ControllerTodo.edit(..))")
    public ModelAndView todoEdit(ProceedingJoinPoint joinPoint) {
        return isLogin(joinPoint);
    }

    /**
     * 个人主页访问权限
     */
    @Around("execution(* web.weibo.controller.ControllerUser.profile(..))")
    public ModelAndView profile(ProceedingJoinPoint joinPoint) {
        return isLogin(joinPoint);
    }
}
